﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace WeiSha.Core.Images
{
	// Token: 0x02000041 RID: 65
	public class ImageTo
	{
		// Token: 0x060002A5 RID: 677 RVA: 0x00012214 File Offset: 0x00010414
		public static Image Cute(Image source, int x, int y, int width, int height)
		{
			int width2 = source.Width;
			int height2 = source.Height;
			width = ((width >= width2) ? width2 : width);
			height = ((height >= height2) ? height2 : height);
			Bitmap bitmap = new Bitmap(width, height);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.DrawImage(source, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
				graphics.Dispose();
			}
			return bitmap;
		}

		// Token: 0x060002A6 RID: 678 RVA: 0x00012298 File Offset: 0x00010498
		public static Image Zoom(Image source, int width, int height, bool isDeformation)
		{
			Image image;
			if (isDeformation)
			{
				image = ImageTo.Zoom(source, width, height);
			}
			else
			{
				double num = (double)width / (double)source.Width;
				double num2 = (double)height / (double)source.Height;
				double num3 = ((num > num2) ? num : num2);
				int num4 = Convert.ToInt32((double)source.Width * num3);
				int num5 = Convert.ToInt32((double)source.Height * num3);
				num4 = ((num4 >= source.Width) ? source.Width : num4);
				num5 = ((num5 >= source.Height) ? source.Height : num5);
				Bitmap bitmap = new Bitmap(width, height);
				using (Graphics graphics = Graphics.FromImage(bitmap))
				{
					graphics.Clear(Color.White);
					graphics.DrawImage(source, (width - num4) / 2, (height - num5) / 2, num4, num5);
					graphics.Dispose();
				}
				image = bitmap;
			}
			return image;
		}

		// Token: 0x060002A7 RID: 679 RVA: 0x0001238C File Offset: 0x0001058C
		public static Image Zoom(Image source, int width, int height)
		{
			Bitmap bitmap = new Bitmap(width, height);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.InterpolationMode = InterpolationMode.High;
				graphics.SmoothingMode = SmoothingMode.HighQuality;
				graphics.DrawImage(source, 0, 0, width, height);
				graphics.Dispose();
			}
			return bitmap;
		}

		// Token: 0x060002A8 RID: 680 RVA: 0x000123F0 File Offset: 0x000105F0
		public static Image Zoom(Image source, int percent)
		{
			int num = source.Width * percent / 100;
			int num2 = source.Height * percent / 100;
			return ImageTo.Zoom(source, num, num2);
		}

		// Token: 0x060002A9 RID: 681 RVA: 0x00012424 File Offset: 0x00010624
		public static Image Thumbnail(Image source, int width, int height)
		{
			int num = source.Width;
			int num2 = source.Height;
			double num3 = (double)width / (double)source.Width;
			double num4 = (double)height / (double)source.Height;
			double num5 = ((num3 > num4) ? num3 : num4);
			num = Convert.ToInt32((double)source.Width * num5);
			num2 = Convert.ToInt32((double)source.Height * num5);
			num = ((num >= source.Width) ? source.Width : num);
			num2 = ((num2 >= source.Height) ? source.Height : num2);
			Bitmap bitmap = new Bitmap(num, num2);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.InterpolationMode = InterpolationMode.High;
				graphics.SmoothingMode = SmoothingMode.HighQuality;
				graphics.DrawImage(source, 0, 0, num, num2);
				graphics.Dispose();
			}
			return ImageTo.Cute(bitmap, (bitmap.Width - width) / 2, (bitmap.Height - height) / 2, width, height);
		}

		// Token: 0x060002AA RID: 682 RVA: 0x00012528 File Offset: 0x00010728
		public static Image Thumbnail(Image source, int percent)
		{
			int num = source.Width * percent / 100;
			int num2 = source.Height * percent / 100;
			return ImageTo.Thumbnail(source, num, num2);
		}

		// Token: 0x060002AB RID: 683 RVA: 0x0001255C File Offset: 0x0001075C
		public static Image Thumbnail(Image source, int width, int height, int restrainObj)
		{
			int num = width;
			int num2 = height;
			bool flag = restrainObj == 0;
			if (flag)
			{
				double num3 = (double)width / (double)source.Width;
				double num4 = (double)height / (double)source.Height;
				double num5 = ((num3 > num4) ? num3 : num4);
				num = Convert.ToInt32((double)source.Width * num5);
				num2 = Convert.ToInt32((double)source.Height * num5);
			}
			bool flag2 = restrainObj == 1;
			if (flag2)
			{
				double num6 = (double)width / (double)source.Width;
				num2 = Convert.ToInt32((double)source.Height * num6);
			}
			bool flag3 = restrainObj == 2;
			if (flag3)
			{
				double num7 = (double)height / (double)source.Height;
				num = Convert.ToInt32((double)source.Width * num7);
			}
			num = ((num >= source.Width) ? source.Width : num);
			num2 = ((num2 >= source.Height) ? source.Height : num2);
			Bitmap bitmap = new Bitmap(width, height);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.Clear(Color.White);
				graphics.DrawImage(source, (width - num) / 2, (height - num2) / 2, num, num2);
				graphics.Save();
				graphics.Dispose();
			}
			return bitmap;
		}

		// Token: 0x060002AC RID: 684 RVA: 0x000126A0 File Offset: 0x000108A0
		public static Image Overlay(Image source, Image img, int x, int y)
		{
			using (Graphics graphics = Graphics.FromImage(source))
			{
				graphics.DrawImage(img, new Rectangle(x, y, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
				graphics.Dispose();
			}
			return source;
		}

		// Token: 0x060002AD RID: 685 RVA: 0x0001270C File Offset: 0x0001090C
		public static Image Overlay(Image source, Image img, int x, int y, int opacity)
		{
			img = ImageTo.Transparent(img, opacity);
			return ImageTo.Overlay(source, img, x, y);
		}

		// Token: 0x060002AE RID: 686 RVA: 0x00012734 File Offset: 0x00010934
		public static Image Overlay(Image source, Image img, string overType, int opacity)
		{
			string text = overType.ToLower();
			string text2 = text;
			uint num = PrivateImplementationDetails.ComputeStringHash(text2);
			int num2;
			int num3;
			if (num <= 2028154341U)
			{
				if (num <= 306900080U)
				{
					if (num != 93078660U)
					{
						if (num == 306900080U)
						{
							if (text2 == "left")
							{
								num2 = 0;
								num3 = (source.Height - img.Height) / 2;
								goto IL_0247;
							}
						}
					}
					else if (!(text2 == "center"))
					{
					}
				}
				else if (num != 1035581717U)
				{
					if (num == 2028154341U)
					{
						if (text2 == "right")
						{
							num2 = source.Width - img.Width;
							num3 = (source.Height - img.Height) / 2;
							goto IL_0247;
						}
					}
				}
				else if (text2 == "down")
				{
					num2 = (source.Width - img.Width) / 2;
					num3 = source.Height - img.Height;
					goto IL_0247;
				}
			}
			else if (num <= 2802900028U)
			{
				if (num != 2770951100U)
				{
					if (num == 2802900028U)
					{
						if (text2 == "top")
						{
							num2 = (source.Width - img.Width) / 2;
							num3 = 0;
							goto IL_0247;
						}
					}
				}
				else if (text2 == "leftdown")
				{
					num2 = 0;
					num3 = source.Height - img.Height;
					goto IL_0247;
				}
			}
			else if (num != 3057180725U)
			{
				if (num != 3614942172U)
				{
					if (num == 4230796363U)
					{
						if (text2 == "lefttop")
						{
							num2 = 0;
							num3 = 0;
							goto IL_0247;
						}
					}
				}
				else if (text2 == "righttop")
				{
					num2 = source.Width - img.Width;
					num3 = 0;
					goto IL_0247;
				}
			}
			else if (text2 == "rightdown")
			{
				num2 = source.Width - img.Width;
				num3 = source.Height - img.Height;
				goto IL_0247;
			}
			num2 = (source.Width - img.Width) / 2;
			num3 = (source.Height - img.Height) / 2;
			IL_0247:
			return ImageTo.Overlay(source, img, num2, num3, opacity);
		}

		// Token: 0x060002AF RID: 687 RVA: 0x00012998 File Offset: 0x00010B98
		public static Image Overlay(Image source, string text, string overType, int opacity)
		{
			Image image = ImageTo.FromString(text, 12, "宋体", "#000");
			return ImageTo.Overlay(source, image, overType, opacity);
		}

		// Token: 0x060002B0 RID: 688 RVA: 0x000129C8 File Offset: 0x00010BC8
		public static Image Overlay(Image source, string text, string overType, int opacity, int size, string font, string color)
		{
			Image image = ImageTo.FromString(text, size, font, color);
			return ImageTo.Overlay(source, image, overType, opacity);
		}

		// Token: 0x060002B1 RID: 689 RVA: 0x000129F0 File Offset: 0x00010BF0
		public static Image FromString(string text)
		{
			int num = text.Length * 18;
			Bitmap bitmap = new Bitmap(num, 18);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				Font font = new Font("宋体", 12f, FontStyle.Bold);
				Brush brush = new SolidBrush(Color.Black);
				graphics.DrawString(text, font, brush, 0f, 0f);
				graphics.Dispose();
			}
			return bitmap;
		}

		// Token: 0x060002B2 RID: 690 RVA: 0x00012A78 File Offset: 0x00010C78
		public static Image FromString(string text, int size, string font, string color)
		{
			font = (string.IsNullOrWhiteSpace(font) ? "宋体" : font);
			string[] array = text.Split(new char[] { '\n' });
			int num = 0;
			foreach (string text2 in array)
			{
				num = ((text2.Length > num) ? text2.Length : num);
			}
			int num2 = (int)((double)num * ((double)((float)size) * 1.5));
			num2 = ((num2 < 1) ? 1 : num2);
			Bitmap bitmap = new Bitmap(num2, (int)Math.Floor((double)(size * array.Length) * 1.5));
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.Clear(Color.White);
				Font font2 = new Font(font, (float)size, FontStyle.Bold);
				Color color2 = ColorTranslator.FromHtml(color);
				Brush brush = new SolidBrush(color2);
				graphics.DrawString(text, font2, brush, 0f, 0f);
				graphics.Dispose();
			}
			return bitmap;
		}

		// Token: 0x060002B3 RID: 691 RVA: 0x00012B94 File Offset: 0x00010D94
		public static Image Transparent(Image img, int opacity)
		{
			opacity = ((opacity < 0) ? 0 : opacity);
			opacity = ((opacity > 100) ? 100 : opacity);
			float[][] array = new float[5][];
			int num = 0;
			float[] array2 = new float[5];
			array2[0] = 1f;
			array[num] = array2;
			int num2 = 1;
			float[] array3 = new float[5];
			array3[1] = 1f;
			array[num2] = array3;
			int num3 = 2;
			float[] array4 = new float[5];
			array4[2] = 1f;
			array[num3] = array4;
			int num4 = 3;
			float[] array5 = new float[5];
			array5[3] = (float)opacity / 100f;
			array[num4] = array5;
			array[4] = new float[] { 0f, 0f, 0f, 0f, 1f };
			float[][] array6 = array;
			ColorMatrix colorMatrix = new ColorMatrix(array6);
			ImageAttributes imageAttributes = new ImageAttributes();
			imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
			Bitmap bitmap = new Bitmap(img.Width, img.Height);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttributes);
				graphics.Dispose();
			}
			return bitmap;
		}

		// Token: 0x060002B4 RID: 692 RVA: 0x00012CA4 File Offset: 0x00010EA4
		public static Image Border(Image img)
		{
			using (Graphics graphics = Graphics.FromImage(img))
			{
				graphics.DrawRectangle(new Pen(Color.Black, 0f), 0, 0, img.Width - 1, img.Height - 1);
				graphics.Dispose();
			}
			return img;
		}

		// Token: 0x060002B5 RID: 693 RVA: 0x00012D0C File Offset: 0x00010F0C
		public static string ToBase64(Image img)
		{
			bool flag = img == null;
			string text;
			if (flag)
			{
				text = string.Empty;
			}
			else
			{
				string text2 = string.Empty;
				Image image = (Image)img.Clone();
				using (Bitmap bitmap = (Bitmap)image)
				{
					using (MemoryStream memoryStream = new MemoryStream())
					{
						bitmap.Save(memoryStream, ImageFormat.Jpeg);
						byte[] array = new byte[memoryStream.Length];
						memoryStream.Position = 0L;
						memoryStream.Read(array, 0, (int)memoryStream.Length);
						memoryStream.Close();
						text2 = Convert.ToBase64String(array);
					}
					bitmap.Dispose();
				}
				text = text2;
			}
			return text;
		}

		// Token: 0x060002B6 RID: 694 RVA: 0x00012DE4 File Offset: 0x00010FE4
		public static Image FromBase64(string base64string)
		{
			byte[] array = Convert.FromBase64String(base64string);
			Bitmap bitmap = null;
			using (MemoryStream memoryStream = new MemoryStream(array))
			{
				bitmap = new Bitmap(memoryStream);
				memoryStream.Dispose();
			}
			return bitmap;
		}

		// Token: 0x060002B7 RID: 695 RVA: 0x00012E34 File Offset: 0x00011034
		public static Image Rounded(Image image)
		{
			return ImageTo.Rounded(image, string.Empty);
		}

		// Token: 0x060002B8 RID: 696 RVA: 0x00012E54 File Offset: 0x00011054
		public static Image Rounded(Image image, string cornerLocation)
		{
			Graphics graphics = Graphics.FromImage(image);
			graphics.SmoothingMode = SmoothingMode.HighQuality;
			graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
			graphics.CompositingQuality = CompositingQuality.HighQuality;
			Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
			GraphicsPath graphicsPath = ImageTo.CreateRoundRectanglePath(rectangle, image.Width / 4, cornerLocation);
			return ImageTo.BitmapCrop((Bitmap)image, graphicsPath);
		}

		// Token: 0x060002B9 RID: 697 RVA: 0x00012EB8 File Offset: 0x000110B8
		private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, string sPosition)
		{
			GraphicsPath graphicsPath = new GraphicsPath();
			if (!(sPosition == "TopLeft"))
			{
				if (!(sPosition == "TopRight"))
				{
					if (!(sPosition == "BottomLeft"))
					{
						if (!(sPosition == "BottomRight"))
						{
							graphicsPath.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180f, 90f);
							graphicsPath.AddLine(rect.X + radius, rect.Y, rect.Right - radius * 2, rect.Y);
							graphicsPath.AddArc(rect.X + rect.Width - radius * 2, rect.Y, radius * 2, radius * 2, 270f, 90f);
							graphicsPath.AddLine(rect.Right, rect.Y + radius * 2, rect.Right, rect.Y + rect.Height - radius * 2);
							graphicsPath.AddArc(rect.X + rect.Width - radius * 2, rect.Y + rect.Height - radius * 2, radius * 2, radius * 2, 0f, 90f);
							graphicsPath.AddLine(rect.Right - radius * 2, rect.Bottom, rect.X + radius * 2, rect.Bottom);
							graphicsPath.AddArc(rect.X, rect.Bottom - radius * 2, radius * 2, radius * 2, 90f, 90f);
							graphicsPath.AddLine(rect.X, rect.Bottom - radius * 2, rect.X, rect.Y + radius * 2);
						}
						else
						{
							graphicsPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0f, 90f);
							graphicsPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
						}
					}
					else
					{
						graphicsPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90f, 90f);
						graphicsPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
					}
				}
				else
				{
					graphicsPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270f, 90f);
					graphicsPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
				}
			}
			else
			{
				graphicsPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180f, 90f);
				graphicsPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
			}
			return graphicsPath;
		}

		// Token: 0x060002BA RID: 698 RVA: 0x000131D8 File Offset: 0x000113D8
		public static Image BitmapCrop(Bitmap bitmap, GraphicsPath path)
		{
			RectangleF bounds = path.GetBounds();
			int num = (int)bounds.Left;
			int num2 = (int)bounds.Top;
			int num3 = (int)bounds.Width;
			int num4 = (int)bounds.Height;
			Bitmap bitmap2 = (Bitmap)bitmap.Clone();
			Bitmap bitmap3 = new Bitmap(num3, num4);
			for (int i = num; i < num + num3; i++)
			{
				for (int j = num2; j < num2 + num4; j++)
				{
					bool flag = path.IsVisible(i, j);
					if (flag)
					{
						bitmap3.SetPixel(i - num, j - num2, bitmap2.GetPixel(i, j));
						bitmap2.SetPixel(i, j, Color.FromArgb(0, bitmap2.GetPixel(i, j)));
					}
					else
					{
						bitmap3.SetPixel(i - num, j - num2, Color.FromArgb(0, 255, 255, 255));
					}
				}
			}
			bitmap.Dispose();
			return bitmap3;
		}
	}
}
